package com.aoxin.service.impl.course.systemCourse;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import com.aoxin.mapper.course.systemCourse.AloneCoursePageDao;
import com.aoxin.mapper.course.systemCourse.SchoolCourseDao;
import com.aoxin.mapper.course.systemCourse.SchoolCoursePageDao;
import com.aoxin.mapper.course.systemCourse.SchoolCourseToPageDao;
import com.aoxin.mapper.course.webCourse.MyCoursePageMapper;
import com.aoxin.model.course.AloneCoursePage;
import com.aoxin.model.opm.SlfEmployee;
import com.aoxin.service.inter.course.systemCourse.AloneCoursePageService;
import com.aoxin.service.inter.course.systemCourse.AloneCourseToPageService;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.ThreadLocalUtil;

@Service("AloneCoursePageService")
public class AloneCoursePageServiceImpl implements AloneCoursePageService {
	@Autowired
	private AloneCoursePageDao aloneCoursePageDao;

	@Autowired
	private AloneCourseToPageService aloneCourseToPageService;
	
	@Autowired
	private SchoolCoursePageDao schoolCoursePageDao;
	
	@Autowired
	private SchoolCourseToPageDao schoolCourseToPageDao;
	
	@Autowired
	private SchoolCourseDao SchoolCourseDao;
	
	@Autowired
	private MyCoursePageMapper slfMyCoursePageMapper;
	/**
	 * 查询课程包（系统管理员）
	 * 
	 * @param aloneCoursePage
	 * @return
	 */
	@Override
	public List<AloneCoursePage> getAloneCoursePage(AloneCoursePage aloneCoursePage,Integer state) {
		if(state == null){
		aloneCoursePage.setState(1);	
		}
		List<AloneCoursePage> list = aloneCoursePageDao.getAloneCoursePage(aloneCoursePage);
		return list;
	}
	
	
	/**
	 * 查询主题课程（系统管理员）
	 * 
	 * @param aloneCoursePage
	 * @return
	 */
	@Override
	public List<AloneCoursePage> getAloneCoursePageC(AloneCoursePage aloneCoursePage) {
		if(aloneCoursePage.getState()==null){
			//默认查询 有效
			aloneCoursePage.setState(1);
		}
		List<AloneCoursePage> list = aloneCoursePageDao.getAloneCoursePageC(aloneCoursePage);
		return list;
	}
	
	/**
	 * 新增课程包(系统管理员)
	 * 
	 * @param aloneCoursePage
	 * @return
	 */
	@Override
	@Transactional
	public void insertAloneCoursePage(AloneCoursePage aloneCoursePage) throws Exception{
		String schoolId = aloneCoursePage.getSchoolId();
		SlfEmployee employee = ThreadLocalUtil.getEmployee();
		if (StringUtils.isEmpty(schoolId) || schoolId.equals(" ") || schoolId.equals("undefined")) {
			schoolId = String.valueOf(employee.getSchoolId());
			Assert.notNull(schoolId, ErrorUtil.SCHOOL_COURSE_SYSTEM);
			aloneCoursePage.setSchoolId(schoolId);
		}
		aloneCoursePage.setEmployeeId(employee.getEmployeeId());
		aloneCoursePage.setEmployeeName(aloneCoursePage.getEmployeeName());
		Integer col = aloneCoursePageDao.checkPage(aloneCoursePage);
		if (col == 0) { 
			aloneCoursePageDao.insertAloneCoursePage(aloneCoursePage);
			aloneCoursePage.setCoursePageIdOther(aloneCoursePageDao.getMax());
			schoolCoursePageDao.insertSchoolCoursePageOther(aloneCoursePage);
		} else {
			throw new Exception(ErrorUtil.CHECK_PAKGE);
		}
		String courseId = aloneCoursePage.getCourseId();
		if (null != courseId) {
			String[] courseIds = courseId.split(",");
			Integer[] course = new Integer[courseIds.length];
			for (int i = 0; i < courseIds.length; i++) {
				course[i] = Integer.valueOf(courseIds[i]);
			}
			this.aloneCourseToPageService.insertCourseToPage(course, aloneCoursePage.getCoursePageId());
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("courseIds",Arrays.asList(course));
			map.put("schoolCoursePageId",schoolCoursePageDao.getMax());
			map.put("schoolId", schoolId);
			map.put("length", course.length);
			//学校课程
			SchoolCourseDao.insertSchoolCourseOther(map);
			List<Integer> list = SchoolCourseDao.getSchoolCourseId(map);
			map.put("list",list);
			schoolCourseToPageDao.insertSchoolCourseToPageOther(map);
			
		}
	}
	/**
	 * 新增主题课程(系统管理员)
	 * 
	 * @param aloneCoursePage
	 * @return
	 */
	@Override
	@Transactional
	public void insertAloneCoursePageC(AloneCoursePage aloneCoursePage) throws Exception{
		String schoolId = aloneCoursePage.getSchoolId();
		SlfEmployee employee = ThreadLocalUtil.getEmployee();
		if (StringUtils.isEmpty(schoolId) || schoolId.equals(" ") || schoolId.equals("undefined")) {
			schoolId = String.valueOf(employee.getSchoolId());
			Assert.notNull(schoolId, ErrorUtil.SCHOOL_COURSE_SYSTEM);
			aloneCoursePage.setSchoolId(schoolId);
		}
		aloneCoursePage.setEmployeeId(employee.getEmployeeId());
		aloneCoursePage.setEmployeeName(aloneCoursePage.getEmployeeName());
		Integer col = aloneCoursePageDao.checkPage(aloneCoursePage);
		if (col == 0) { 
			aloneCoursePageDao.insertAloneCoursePage(aloneCoursePage);
			aloneCoursePage.setCoursePageIdOther(aloneCoursePageDao.getMax());
			schoolCoursePageDao.insertSchoolCoursePageOther(aloneCoursePage);
		} else {
			throw new Exception(ErrorUtil.CHECK_PAKGE);
		}
	}

	/**
	 * 修改课程包
	 * 
	 * @param aloneCoursePage
	 * @return
	 */
	@Override
	@Transactional
	public Integer updateAloneCoursePage(AloneCoursePage aloneCoursePage) throws Exception {
		String schoolId = aloneCoursePage.getSchoolId();
		SlfEmployee employee = ThreadLocalUtil.getEmployee();
		if (StringUtils.isEmpty(schoolId) || schoolId.equals(" ") || schoolId.equals("undefined")) {
			schoolId = String.valueOf(employee.getSchoolId());
			Assert.notNull(schoolId, ErrorUtil.SCHOOL_COURSE_SYSTEM);
			aloneCoursePage.setSchoolId(schoolId);
		}
		aloneCoursePage.setEmployeeId(employee.getEmployeeId());
		aloneCoursePage.setEmployeeName(aloneCoursePage.getEmployeeName());
		Integer col = aloneCoursePageDao.checkPage(aloneCoursePage);
		if (col == 0) {
			//判断是否有购买的(slf_my_course_page)
			Integer b = slfMyCoursePageMapper.IsPayCoursePage(aloneCoursePage.getCoursePageId());
			if(b>0){
			throw new Exception(ErrorUtil.PAYARLEARY);	
			}else{
			this.schoolCoursePageDao.updateSchoolCoursePageOther(aloneCoursePage);	
			return aloneCoursePageDao.updateAloneCoursePage(aloneCoursePage);
			}
		} else {
			throw new Exception(ErrorUtil.CHECK_PAKGE);
		}
	}
	/**
	 * 修改主题课程
	 * 
	 * @param aloneCoursePage
	 * @return
	 */
	@Override
	@Transactional
	public Integer updateAloneCoursePageC(AloneCoursePage aloneCoursePage) throws Exception {
		String schoolId = aloneCoursePage.getSchoolId();
		SlfEmployee employee = ThreadLocalUtil.getEmployee();
		if (StringUtils.isEmpty(schoolId) || schoolId.equals(" ") || schoolId.equals("undefined")) {
			schoolId = String.valueOf(employee.getSchoolId());
			Assert.notNull(schoolId, ErrorUtil.SCHOOL_COURSE_SYSTEM);
			aloneCoursePage.setSchoolId(schoolId);
		}
		aloneCoursePage.setEmployeeId(employee.getEmployeeId());
		aloneCoursePage.setEmployeeName(aloneCoursePage.getEmployeeName());
		Integer col = aloneCoursePageDao.checkPage(aloneCoursePage);
		if (col == 0) {
			//已被用户购买的主题课不能修改
			if("true".equals(aloneCoursePageDao.queryMyCoursePageExistenceCoursePageC(aloneCoursePage.getCoursePageId()))){
				throw new Exception("已被用户购买课程不予许修改！");
			}
			return aloneCoursePageDao.updateAloneCoursePage(aloneCoursePage);
		} else {
			throw new Exception(ErrorUtil.CHECK_PAKGE);
		}
	}
	
	/**
	 * 删除课程包
	 * 
	 * @param coursePageId
	 * @return
	 */
	@Override
	@Transactional
	public Integer deletePage(Integer coursePageId) throws Exception {
		Integer col = aloneCoursePageDao.deletePage(coursePageId);
		if (col > 0) {
			return col;
		} else {
			throw new Exception(ErrorUtil.RESULT_VALUE);
		}

	}

    //修改启用或者禁用
	@Override
	@Transactional
	public void updateAloneCoursePageState(List<Integer> coursePageIds,Integer state) {
		Assert.notEmpty(coursePageIds, String.format(ErrorUtil.PARAMETER_NULL, "coursePageIds"));
		Assert.notNull(state, String.format(ErrorUtil.PARAMETER_NULL, "state"));
		if(state == 0){
		for(Integer coursePageId : coursePageIds){
			//判断似乎否绑定了包,绑定了则不能被随意修改
			Integer b = slfMyCoursePageMapper.IsPayCoursePage(coursePageId);
			if(b>0){
			throw new IllegalArgumentException(ErrorUtil.PAYARLEARY);
			}
		}
		}
		this.aloneCoursePageDao.updateState(coursePageIds,state);
		this.schoolCoursePageDao.updateState(coursePageIds,state);
	}


	@Override
	public void updateAloneCoursePageCState(List<Integer> coursePageIds,Integer state) throws Exception {
		if(state==0){
			for (int i = 0,b=coursePageIds.size(); i < b; i++) {
				//已被用户购买的主题课不能修改
				if("true".equals(aloneCoursePageDao.queryMyCoursePageExistenceCoursePageC(coursePageIds.get(i)))){
					throw new Exception("已被用户购买课程不予许修改！");
				}
			}
		}
		aloneCoursePageDao.updateCoursePageCState(coursePageIds, state);
	}
	
}
